package com.tianqu.tms.www.transfer.action;

import com.tianqu.LicensePlateConfigConstant;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.www.transfer.actionform.CheckConfirmActionForm;
import com.tianqu.tms.www.transfer.controller.BussinessCardInputResultController;
import com.tianqu.tms.www.transfer.viewhelper.CheckConfirmViewHelper;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

//20160405
public class BussinessCardSearchResultAction extends TmsAction {
	private Logger logger = Logger.getLogger(BussinessCardSearchResultAction.class);
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");	
		CheckConfirmActionForm inputForm = (CheckConfirmActionForm) form;
		CheckConfirmViewHelper view =(CheckConfirmViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
		String serial = inputForm.getSerial();
        view.setSerial(inputForm.getSerial());
        view.setChangeLicensePlate(inputForm.getChangeLicensePlate());
        view.setNewLicensePlate(LicensePlateConfigConstant.defLicensePlate+inputForm.getNewLicensePlate());
		
		TransferAcceptedInfo accept = new TransferAcceptedInfo();
//        isToken(mapping, request);
        // 
//        TransferRgInputViewHelper view = (TransferRgInputViewHelper) uc.getAttribute(TmsConstants.VIEWHELP);
        List<TransferAcceptedInfo> infoList = accept.getAcceptInfoList(serial, "");
        /*
         * 1）输入流水号查询：通过车辆流水号（serial）为录入的流水号 + 车辆过户状态（status）不为“0：业务作废，16：过户完了”，到过户业务基本信息表 (tms_transfer_accepted_info)进行查询
         * # 如果没有符合条件数据，那么页面提示“【U0039】查询的结果不存在。”
         * # 如果有多条数据，那么页面提示“【U1405】出现重复流水号业务，请联系系统管理员。”
         * 2）如果流水号都不输入，点击查询，页面提示“【U0040】必须输入一个查询条件。” --- validation-transfer.xml
         */
        if (infoList != null) {
            if (infoList.size() == 1) {
                accept  = infoList.get(0);
            } else if (infoList.size() == 0) {
                setErrorsToRequest(request, TmsConstants.U0039 ,null);
                return mapping.findForward(TmsConstants.SUCCESS);
            } else {
                setErrorsToRequest(request, TmsConstants.U1405 ,null);
                return mapping.findForward(TmsConstants.SUCCESS);
            }
        } else {
            setErrorsToRequest(request, TmsConstants.U0039 ,null);
            return mapping.findForward(TmsConstants.SUCCESS);
        }
        BussinessCardInputResultController bcirc = new BussinessCardInputResultController();
        bcirc.execute(uc);
		uc.setAttribute(TmsConstants.VIEWHELP, view);
		logger.debug(this.getClass().getName() + "#execute was completd.");	
		return mapping.findForward(TmsConstants.SUCCESS);
	}
	
	public boolean isNotLicensePlate(String licensePlate){
		if(StringUtil.isNullOrBlank(licensePlate)) return true;
		String tmp = licensePlate.replace("挂", "");
		if(tmp.matches("^[a-zA-Z0-9]+$") && tmp.length()<=5) return false;
		else return true;
	}
}
